﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using System.Text;

namespace UnitTestsScenarioEditor.Fileformats
{
    [TestClass]
    public class C11UndercoverTest
    {
        #region Public Methods

        [TestInitialize()]
        public void Initialize()
        {
            // MessageBox.Show("C11 Test init");
            //Assert.Fail();
        }

        [TestMethod()]
        public void TestAlpenQadScenerario()
        {
            string filenameToQad = @"D:\Games\Alarm für Cobra 11 - Undercover\Scenarios\Alpen\V1\Alpen.qad";
            string FilenameSaved = "AlpenQadTest.qad";

            LoadAndTestQadFile(filenameToQad, FilenameSaved);
        }

        [TestMethod()]
        public void TestAutobahnQadScenerario()
        {
            string filenameToQad = @"D:\Games\Alarm für Cobra 11 - Undercover\Scenarios\Autobahn\V1\Autobahn.qad";
            string FilenameSaved = "AutobahnQadTest.qad";

            LoadAndTestQadFile(filenameToQad, FilenameSaved);
        }

        [TestMethod()]
        public void TestLight()
        {
            //MessageBox.Show("Light Test");
            // Assert.Fail();
        }

        [TestMethod()]
        public void TestRennstreckeQadScenerario()
        {
            string filenameToQad = @"D:\Games\Alarm für Cobra 11 - Undercover\Scenarios\Rennstrecke\V1\Rennstrecke.qad";
            string FilenameSaved = "RennstreckeQadTest.qad";

            LoadAndTestQadFile(filenameToQad, FilenameSaved);
        }

        [TestMethod()]
        public void TestStadiumQadScenerario()
        {
            string filenameToQad = @"D:\Games\Alarm für Cobra 11 - Undercover\Scenarios\Stadium\V1\Stadium.qad";
            string FilenameSaved = "StadiumQadTest.qad";

            LoadAndTestQadFile(filenameToQad, FilenameSaved);
        }

        #endregion Public Methods

        #region Private Methods

        private static void LoadAndTestQadFile(string filenameToQad, string FilenameSaved)
        {
            int numberOfErrors = 0;

            try
            {
                StringBuilder sbErrorReport = new StringBuilder();
                Engine.FileFormats3D.C11Undercover.QadFile c11qad = Engine.FileFormats3D.C11Undercover.QadFile.FromFile(filenameToQad);
                c11qad.ToFile(FilenameSaved);

                FileStream fs = new FileStream(FilenameSaved, FileMode.Open);
                BinaryReader brNewQad = new BinaryReader(fs);

                FileStream fsOriginal = new FileStream(filenameToQad, FileMode.Open);
                BinaryReader brOriginalQad = new BinaryReader(fsOriginal);

                sbErrorReport.AppendLine("Original: " + fsOriginal.Length.ToString().PadRight(16) + " " + filenameToQad);
                sbErrorReport.AppendLine("New Qad:  " + fs.Length.ToString().PadRight(16) + " " + FilenameSaved);
                sbErrorReport.AppendLine("Pos=============Original=====New==============\n");
                if (brNewQad.BaseStream.Length != brOriginalQad.BaseStream.Length)
                {
                    Console.WriteLine("Wrong file sizes !!");
                }

                for (int i = 0; i < Math.Min(brNewQad.BaseStream.Length, brOriginalQad.BaseStream.Length); i++)
                {
                    byte byteOriginal = brOriginalQad.ReadByte();
                    byte byteNew = brNewQad.ReadByte();

                    if (byteNew != byteOriginal)
                    {
                        numberOfErrors++;
                        sbErrorReport.AppendLine((brNewQad.BaseStream.Position - 1).ToString().PadRight(16, ' ') + byteOriginal.ToString().PadRight(4) + " " + byteNew.ToString().PadRight(4));
                    }
                }

                if (numberOfErrors > 0)
                {
                    string errorFilename = "Error_" + FilenameSaved + ".txt";
                    using (StreamWriter sw = new StreamWriter(errorFilename))
                    {
                        sw.Write(sbErrorReport.ToString());
                    }

                    Assert.Fail("See Error file!  " + errorFilename);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }

        #endregion Private Methods
    }
}